package com.neu.yth.nepBackend.member.controller;

import com.neu.yth.nepBackend.entity.Member;
import com.neu.yth.nepBackend.entity.PageInfo;
import com.neu.yth.nepBackend.member.service.IMemberService;
import com.neu.yth.nepBackend.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 成员信息表（管理员、公众监督员、网格员） 前端控制器
 * </p>
 *
 * @author YTH
 * @since 2024-05-31
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService memberService;

    /**
     * 获取所有成员列表
     *
     * @return
     */
    @GetMapping("/select/all")
    public Result<List<Member>> selectAll() {
        return memberService.selectAll();
    }

    /**
     * 分页获取所有成员列表
     *
     * @return
     */
    @PostMapping("/select/page")
    public Result selectPage(@RequestBody PageInfo pageInfo) {
        return memberService.selectPage(pageInfo);
    }

    /**
     * 分页获取所有网格员列表
     *
     * @return
     */
    @PostMapping("/select/gridPage")
    public Result selectGridPage(@RequestBody PageInfo pageInfo) {
        return memberService.selectGridPage(pageInfo);
    }

    /**
     * 获取所有网格员列表
     *
     * @return
     */
    @GetMapping("/select/grid")
    public Result<List<Member>> selectGrid() {
        return memberService.selectGrid();
    }


    /**
     * 根据id查询
     *
     * @param memId
     * @return
     */
    @GetMapping("/select/id")
    public Result<Member> selectById(@RequestParam Integer memId) {
        return memberService.selectById(memId);
    }

    /**
     * 账号密码登录
     *
     * @param logInID
     * @param logInPwd
     * @return
     */
    @PostMapping("/login/{logInID}")
    public Result<Member> login(@PathVariable("logInID") String logInID, @RequestBody String logInPwd) {
        return memberService.login(logInID, logInPwd);
    }

    /**
     * 账号、验证码登录
     *
     * @param member
     * @return
     */
    @PostMapping("/login/code")
    public Result loginCode(@RequestBody Member member) {
        return memberService.loginCode(member);
    }

    /**
     * 获取验证码
     *
     * @param tel
     * @return
     */
    @GetMapping("/login/code/get/{tel}")
    public Result getCode(@PathVariable String tel) {
        return memberService.getCode(tel);
    }


    /**
     * 注册
     *
     * @param member
     * @return
     */
    @PostMapping("/register")
    public Result<Member> register(@RequestBody Member member) {
        return memberService.register(member);
    }


    /**
     * 更新
     *
     * @param member
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Member member) {
        return memberService.updateMember(member);
    }

    /**
     * 删除
     *
     * @param memId
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("memId") String memId) {
        return memberService.deleteMember(memId);
    }


    /**
     * 测试(分配任务时修改网格员状态)
     *
     * @param memId
     * @return
     */
    @GetMapping("/update/assign/{memId}")
    public Result updateStatus(@PathVariable Integer memId) {
        return memberService.updateAssign(memId);
        //System.out.println("token:"+token);
        //return Result.fail(200,"成功",token);
        //@RequestHeader(value = "Authorization") String token
    }

    /**
     * 根据角色ID查询用户数
     *
     * @param roleId
     * @return
     */
    @GetMapping("/select/count/{roleId}")
    public Result selectCount(@PathVariable Integer roleId) {
        return memberService.selectCount(roleId);
    }


    /**
     * 选择自动分配网格员
     *
     * @param location
     * @return
     */
    @GetMapping("/select/autoAssign")
    public Result selectAutoAssign(@RequestParam String location) {
        return memberService.selectAutoAssign(location);
    }
}
